package cn.socialcredits.qqcrawler;

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class QGroupUserCrawler {

	public static int MOUSE_LEFT = InputEvent.BUTTON1_MASK;
	public static int MOUSE_RIGHT = InputEvent.BUTTON2_MASK;
	public static int MOUSE_MIDDLE = InputEvent.BUTTON3_MASK;
	private static int DOUBLE_SCREEN = 1920;

	private static Robot robot;
	static {
		try {
			robot = new Robot();
		} catch (Exception e) {
		}
	}

	public static void sleep(int time) {
		try {
			Thread.sleep(time);
		} catch (Exception e) {
		}
	}

	public void move(int x, int y) {
		robot.mouseMove(x, y);
	}

	public void click(int button) {
		robot.mousePress(button);
		robot.mouseRelease(button);
	}

	public void pressKey(int keycode) {
		robot.keyPress(keycode);
		robot.keyRelease(keycode);
	}

	public String chooseText() {
		click(QGroupUserCrawler.MOUSE_LEFT);
		sleep(150);
		robot.keyPress(KeyEvent.VK_CONTROL);
		pressKey(KeyEvent.VK_A);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		sleep(500);
		robot.keyPress(KeyEvent.VK_CONTROL);
		pressKey(KeyEvent.VK_C);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		sleep(500);

		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		Transferable transferable = clipboard.getContents(null);
		if (transferable != null) {
			if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
				try {
					return (String) transferable.getTransferData(DataFlavor.stringFlavor);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return "";
	}
	
	private void doubleClick(){
		click(MOUSE_LEFT);
		sleep(50);
		click(MOUSE_LEFT);
	}
	
	private String checkNowUser(){
		doubleClick();
		sleep(130);
		move(DOUBLE_SCREEN + 780, 285);
		click(MOUSE_LEFT);
		sleep(70);
		move(DOUBLE_SCREEN + 1258, 285);
		sleep(70);
		click(MOUSE_LEFT);
		sleep(70);
		move(DOUBLE_SCREEN + 850, 442);
		sleep(70);
		String result = chooseText();
		move(DOUBLE_SCREEN + 1194, 220);
		sleep(100);
		click(MOUSE_LEFT);
		return result;
	}
	
	private List<String> runPage(){
		List<String> qqList = new ArrayList<String>(8);
		int initX = DOUBLE_SCREEN + 830;
		int initY = 520;
		move(initX, initY);
		for (int i=0; i<8; i++){
			System.out.println("looping");
			move(initX, initY + i * 38);
			sleep(100);
			qqList.add((checkNowUser()));
			sleep(1000);
		}
		
		return qqList;
	}
	
	private void save(String qzone, List<String> list) throws Exception{
		FileWriter fw = new FileWriter(new File(qzone));
		for (String qqnum : list){
			fw.write(qqnum + "\n");
		}
		fw.close();
	}

	public String run(String qzone){
		List<String> list = new ArrayList<String>();
		int saved = 0;
		while (true) {
			for (String qqnum : runPage()){
				if (list.indexOf(qqnum) >= 0) {
					saved += 1;
				} else {
					list.add(qqnum);
				}
			}
			sleep(500);
			move(DOUBLE_SCREEN + 830, 520);
			sleep(500);
			robot.mouseWheel(3);
			sleep(500);
			try{
				save(qzone, list);
			} catch (Exception e){
				
			}
			if (saved >= 8){
				break;
			}
		}
		return "test";
	}

	public static void main(String[] args) { 
		System.out.println(new QGroupUserCrawler().run("298597040"));
		
		new Thread(){
			public void run() {}
		}.start();
		
	}
}
